﻿using System.CodeDom;
using System.Collections.Generic;

namespace NRemedy
{
    public class CSharpModelGenerateCode
    {
        public CodeCompileUnit GenerateClassByFormName(IARSchema asrsch, string FormName, CSharpModelGeneratorDefaultFactory csmgdf)
        {
            CodeCompileUnit ccu = new CodeCompileUnit();
            IGenerateNameResolver gnr = csmgdf.CreateGenerateNameResolver();
            //root namespace
            IGenerateRootNamespace rn = csmgdf.CreateGenerateRootNamespace();
            rn.Create(ccu);
            //import namespace
            IGenerateImportNamespace imn = csmgdf.CreateGenerateImportNamespace();
            imn.Create(ccu.Namespaces[0]);
            //class
            IGenerateClass gc = csmgdf.CreateGenerateClass();
            IGenerateClassAttribute igca = csmgdf.CreateGenerateClassAttribute();
            gc.Create(ccu.Namespaces[0], gnr.DefaultClassNameResolver(FormName), FormName, igca);
            //field
            IGetARFieldFilterDelegate affd = csmgdf.CreateGetARFieldFilterDelegate();
            var fields = asrsch.GetListField(FormName, affd.getARFieldFilterDelegate());
            IGetGenerateFieldDelegate igfd = csmgdf.CreateGenerateFieldDelegate();
            GenerateFieldDelegate gfd = igfd.getGenerateFieldDelegate();
            foreach (var f1 in fields)
            {
                IGenerateFieldFactory igff = csmgdf.CreateGenerateFieldFactory();
                IGenerateField igf = igff.getGenerateFieldCreate((ARType)f1.DataType);
                igf.Create(ccu.Namespaces[0].Types[0], gnr.DefaultFieldNameResolver(f1.Name), ARTypeConvert.MappingARType(f1.DataType).Name, gnr.DefaultPropertyNameResolver(f1.Name), gfd);
            }
            //property
            Dictionary<string, object> mapping = new Dictionary<string, object>();
            IGetGeneratePropertyDelegate igpd = csmgdf.CreateGeneratePropertyDelegate();
            GeneratePropertyDelegate gpd = igpd.getGeneratePropertyDelegate();
            IGeneratePropertyAttribute igpa = csmgdf.CreateGeneratePropertyAttribute();
            foreach (var f2 in fields)
            {
                mapping.Clear();
                mapping.Add("DatabaseID", f2.Id);
                mapping.Add("DatabaseName", f2.Name);
                mapping.Add("DataType", (ARType)f2.DataType);
                if(f2.FieldOption==BMC.ARSystem.Constants.AR_FIELD_OPTION_DISPLAY)
                {
                    mapping.Add("BinderAccess", ModelBinderAccessLevel.OnlyUnBind);
                }
                BMC.ARSystem.Limit limit = f2.Limit;
                IGeneratePropertyFactory igpf = csmgdf.CreateGeneratePropertyFactory();
                IGenerateProperty igp = igpf.getGeneratePropertyCreate((ARType)f2.DataType);
                igp.Create(ccu.Namespaces[0].Types[0], gnr.DefaultPropertyNameResolver(f2.Name), ARTypeConvert.MappingARType(f2.DataType).Name, limit, mapping, gpd, igpa);
            }
            return ccu;
        }
    }
}
